package com.example.demo.controller;

import com.example.demo.dto.WorkflowTaskTypeDTO;
import com.example.demo.dto.TaskTypeApprovalStatisticsDTO;
import com.example.demo.response.ApiResponse;
import com.example.demo.service.WorkflowTaskTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/workflow-task-types")

public class WorkflowTaskTypeController {

    @Autowired
    private WorkflowTaskTypeService workflowTaskTypeService;

    /**
     * 获取所有流程任务映射
     */

    @GetMapping
    public ResponseEntity<ApiResponse<List<WorkflowTaskTypeDTO>>> getAllWorkflowTaskTypes(HttpServletRequest request) {
        List<WorkflowTaskTypeDTO> workflowTaskTypes = workflowTaskTypeService.getAllWorkflowTaskTypes();
        return ResponseEntity.ok(
                ApiResponse.success(workflowTaskTypes, "请求成功", request.getRequestURI(), null)
        );
    }

    /**
     * 根据ID获取流程任务映射
     */

    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<WorkflowTaskTypeDTO>> getWorkflowTaskTypeById(
             @PathVariable Long id,
            HttpServletRequest request) {
        WorkflowTaskTypeDTO workflowTaskType = workflowTaskTypeService.getWorkflowTaskTypeById(id);
        return ResponseEntity.ok(
                ApiResponse.success(workflowTaskType, "查询成功", request.getRequestURI(), null)
        );
    }

    /**
     * 根据流程ID获取流程任务映射列表
     */

    @GetMapping("/workflow/{workflowId}")
    public ResponseEntity<ApiResponse<List<WorkflowTaskTypeDTO>>> getWorkflowTaskTypesByWorkflowId(
             @PathVariable Long workflowId,
            HttpServletRequest request) {
        List<WorkflowTaskTypeDTO> workflowTaskTypes = workflowTaskTypeService.getWorkflowTaskTypesByWorkflowId(workflowId);
        return ResponseEntity.ok(
                ApiResponse.success(workflowTaskTypes, "查询成功", request.getRequestURI(), null)
        );
    }

    /**
     * 根据任务类型ID获取流程任务映射列表
     */

    @GetMapping("/task-type/{taskTypeId}")
    public ResponseEntity<ApiResponse<List<WorkflowTaskTypeDTO>>> getWorkflowTaskTypesByTaskTypeId(
             @PathVariable Long taskTypeId,
            HttpServletRequest request) {
        List<WorkflowTaskTypeDTO> workflowTaskTypes = workflowTaskTypeService.getWorkflowTaskTypesByTaskTypeId(taskTypeId);
        return ResponseEntity.ok(
                ApiResponse.success(workflowTaskTypes, "查询成功", request.getRequestURI(), null)
        );
    }

    /**
     * 根据数据源ID获取流程任务映射列表
     */

    @GetMapping("/datasource/{datasourceId}")
    public ResponseEntity<ApiResponse<List<WorkflowTaskTypeDTO>>> getWorkflowTaskTypesByDatasourceId(
             @PathVariable Long datasourceId,
            HttpServletRequest request) {
        List<WorkflowTaskTypeDTO> workflowTaskTypes = workflowTaskTypeService.getWorkflowTaskTypesByDatasourceId(datasourceId);
        return ResponseEntity.ok(
                ApiResponse.success(workflowTaskTypes, "查询成功", request.getRequestURI(), null)
        );
    }

    /**
     * 创建流程任务映射
     */

    @PostMapping
    public ResponseEntity<ApiResponse<WorkflowTaskTypeDTO>> createWorkflowTaskType(
             @RequestBody WorkflowTaskTypeDTO workflowTaskTypeDTO,
            HttpServletRequest request) {
        WorkflowTaskTypeDTO created = workflowTaskTypeService.createWorkflowTaskType(workflowTaskTypeDTO);
        return ResponseEntity.status(201).body(
                ApiResponse.created(created, "创建成功", request.getRequestURI())
        );
    }

    /**
     * 批量创建流程任务映射
     */

    @PostMapping("/batch")
    public ResponseEntity<ApiResponse<List<WorkflowTaskTypeDTO>>> batchCreateWorkflowTaskTypes(
            @RequestBody List<WorkflowTaskTypeDTO> workflowTaskTypeDTOs,
            HttpServletRequest request) {
        List<WorkflowTaskTypeDTO> created = workflowTaskTypeService.batchCreateWorkflowTaskTypes(workflowTaskTypeDTOs);
        return ResponseEntity.status(201).body(
                ApiResponse.created(created, "批量创建成功", request.getRequestURI())
        );
    }

    /**
     * 更新流程任务映射
     */

    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<WorkflowTaskTypeDTO>> updateWorkflowTaskType(
           @PathVariable Long id,
             @RequestBody WorkflowTaskTypeDTO workflowTaskTypeDTO,
            HttpServletRequest request) {
        WorkflowTaskTypeDTO updated = workflowTaskTypeService.updateWorkflowTaskType(id, workflowTaskTypeDTO);
        return ResponseEntity.ok(
                ApiResponse.success(updated, "更新成功", request.getRequestURI(), null)
        );
    }

    /**
     * 删除流程任务映射
     */

    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteWorkflowTaskType(
             @PathVariable Long id,
            HttpServletRequest request) {
        workflowTaskTypeService.deleteWorkflowTaskType(id);
        return ResponseEntity.ok(
                ApiResponse.success(null, "删除成功", request.getRequestURI(), null)
        );
    }

    /**
     * 根据流程ID删除流程任务映射
     */

    @DeleteMapping("/workflow/{workflowId}")
    public ResponseEntity<ApiResponse<Void>> deleteWorkflowTaskTypesByWorkflowId(
            @PathVariable Long workflowId,
            HttpServletRequest request) {
        workflowTaskTypeService.deleteWorkflowTaskTypesByWorkflowId(workflowId);
        return ResponseEntity.ok(
                ApiResponse.success(null, "删除成功", request.getRequestURI(), null)
        );
    }

    /**
     * 根据流程ID和任务类型ID删除流程任务映射
     */

    @DeleteMapping("/workflow/{workflowId}/task-type/{taskTypeId}")
    public ResponseEntity<ApiResponse<Void>> deleteWorkflowTaskTypeByWorkflowIdAndTaskTypeId(
             @PathVariable Long workflowId,
             @PathVariable Long taskTypeId,
            HttpServletRequest request) {
        workflowTaskTypeService.deleteWorkflowTaskTypeByWorkflowIdAndTaskTypeId(workflowId, taskTypeId);
        return ResponseEntity.ok(
                ApiResponse.success(null, "删除成功", request.getRequestURI(), null)
        );
    }

    /**
     * 统计流程任务映射数量
     */

    @GetMapping("/count")
    public ResponseEntity<ApiResponse<Long>> countWorkflowTaskTypes(HttpServletRequest request) {
        long count = workflowTaskTypeService.countWorkflowTaskTypes();
        return ResponseEntity.ok(
                ApiResponse.success(count, "统计成功", request.getRequestURI(), null)
        );
    }
    
    /**
     * 根据任务类型ID、版本、部门和应用信息获取任务审批统计信息
     */

    @GetMapping("/{taskTypeId}/statistics")
    public ResponseEntity<ApiResponse<TaskTypeApprovalStatisticsDTO>> getTaskTypeApprovalStatistics(
             @PathVariable Long taskTypeId,
             @RequestParam(required = false) String version,
             @RequestParam(required = false) String dep,
            @RequestParam(required = false) String app,
            HttpServletRequest request) {
        TaskTypeApprovalStatisticsDTO statistics = workflowTaskTypeService.getTaskTypeApprovalStatistics(taskTypeId, version, dep, app);
        return ResponseEntity.ok(
                ApiResponse.success(statistics, "查询成功", request.getRequestURI(), null)
        );
    }
    
    /**
     * 根据工作流ID、版本、部门和应用信息获取工作流任务审批统计信息
     */

    @GetMapping("/workflow/{workflowId}/statistics")
    public ResponseEntity<ApiResponse<List<TaskTypeApprovalStatisticsDTO>>> getWorkflowApprovalStatistics(
             @PathVariable Long workflowId,
             @RequestParam(required = false) String version,
            @RequestParam(required = false) String dep,
             @RequestParam(required = false) String app,
            HttpServletRequest request) {
        List<TaskTypeApprovalStatisticsDTO> statistics = workflowTaskTypeService.getWorkflowApprovalStatistics(workflowId, version, dep, app);
        return ResponseEntity.ok(
                ApiResponse.success(statistics, "查询成功", request.getRequestURI(), null)
        );
    }
}